package com.voltmao.webpro.utils;

import java.util.Random;

/**
 * Created by zhouyang on 2017/5/9.
 */
public class NumberUtils {

    public static int ramdomBetween(int min, int max) {
        Random random = new Random();

        return random.nextInt(max) % (max - min + 1) + min;
    }


    /**
     * 四舍五入
     *
     * @return
     */
    public static double getRoundDouble(double value) {

        return Math.round(value * 100) / 100.0;
    }

    public static int parseInt(Object obj) {
        String num = obj + "";

        try {
            return (int) Double.parseDouble(num);
        } catch (Exception ex) {
            return 0;
        }
    }

    public static int parseInt(String obj, int defualt) {


        try {
            return (int) Double.parseDouble(obj);
        } catch (Exception ex) {
            return defualt;
        }
    }

    /**
     * 字符串转换为double,如不能转换设置为默认值
     *
     * @param str
     * @param defaultValue
     * @return
     */
    public static Double parseDouble(String str, Double defaultValue) {

        if (str == null || str.length() == 0) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(str);
        } catch (Throwable e) {
            return defaultValue;
        }
    }


    public static void main(String[] args) {

        System.out.println(parseInt("1.0"));
    }
}
